<link rel="import" href="../../../../packages/polymer/polymer.html">
<link rel="import" href="curly_block.html">
<link rel="import" href="eval_box.html">
<link rel="import" href="eval_link.html">
<link rel="import" href="field_ref.html">
<link rel="import" href="function_ref.html">
<link rel="import" href="instance_ref.html">
<link rel="import" href="library_ref.html">
<link rel="import" href="nav_bar.html">
<link rel="import" href="observatory_element.html">
<link rel="import" href="script_inset.html">
<link rel="import" href="script_ref.html">
<link rel="import" href="view_footer.html">

<polymer-element name="class-view" extends="observatory-element">
  <template>
    <link rel="stylesheet" href="css/shared.css">
    <nav-bar>
      <top-nav-menu></top-nav-menu>
      <vm-nav-menu vm="{{ cls.isolate.vm }}"></vm-nav-menu>
      <isolate-nav-menu isolate="{{ cls.isolate }}"></isolate-nav-menu>
      <library-nav-menu library="{{ cls.library }}"></library-nav-menu>
      <class-nav-menu cls="{{ cls }}" last="{{ true }}"></class-nav-menu>
      <nav-refresh callback="{{ refreshCoverage }}" label="Refresh Coverage"></nav-refresh>
      <nav-refresh callback="{{ refresh }}"></nav-refresh>
    </nav-bar>

    <div class="content">
      <h1>
        <template if="{{ cls.isAbstract }}">
          abstract
        </template>
        <template if="{{ cls.isPatch }}">
          patch
        </template>
        class {{ cls.name }}
      </h1>
      <div class="memberList">
        <div class="memberItem">
          <div class="memberName">library</div>
          <div class="memberValue">
            <library-ref ref="{{ cls.library }}"></library-ref>
          </div>
        </div>
        <div class="memberItem">
          <div class="memberName">script</div>
          <div class="memberValue">
            <source-link location="{{ cls.location }}"></source-link>
          </div>
        </div>

        <div class="memberItem">&nbsp;</div>

        <template if="{{ cls.superclass != null }}">
          <div class="memberItem">
            <div class="memberName">extends</div>
            <div class="memberValue">
              <class-ref ref="{{ cls.superclass }}"></class-ref>
            </div>
          </div>
        </template>
        <template if="{{ cls.subclasses.length > 0 }}">
          <div class="memberItem">
            <div class="memberName">extended by</div>
            <div class="memberValue">
              <template repeat="{{ subclass in cls.subclasses }}">
                <class-ref ref="{{ subclass }}"></class-ref>
              </template>
            </div>
          </div>
        </template>

        <div class="memberItem">&nbsp;</div>

        <template if="{{ cls.interfaces.length > 0 }}">
          <div class="memberItem">
            <div class="memberName">implements</div>
            <div class="memberValue">
              <template repeat="{{ interface in cls.interfaces }}">
                <instance-ref ref="{{ interface }}"></instance-ref>
              </template>
            </div>
          </div>
        </template>
        <template if="{{ cls.name != cls.vmName }}">
          <div class="memberItem">
            <div class="memberName">vm name</div>
            <div class="memberValue">{{ cls.vmName }}</div>
          </div>
        </template>
      </div>
    </div>

    <template if="{{ cls.error != null }}">
      <error-ref ref="{{ cls.error }}"></error-ref>
    </template>

    <hr>

    <div class="content">
      <eval-box callback="{{ evaluate }}"></eval-box>
    </div>

    <hr>

    <div class="content">
      <template if="{{ cls.fields.isNotEmpty }}">
        fields ({{ cls.fields.length }})
        <curly-block expand="{{ cls.fields.length <= 8 }}">
          <div class="memberList">
            <template repeat="{{ field in cls.fields }}">
              <div class="memberItem">
                <div class="memberName">
                  <field-ref ref="{{ field }}"></field-ref>
                </div>
                <div class="memberValue">
                  <template if="{{ field.staticValue != null }}">
                    <any-service-ref ref="{{ field.staticValue }}"></any-service-ref>
                  </template>
                </div>
              </div>
            </template>
          </div>
        </curly-block><br><br>
      </template>

      <template if="{{ cls.functions.isNotEmpty }}">
        functions ({{ cls.functions.length }})
        <curly-block expand="{{ cls.functions.length <= 8 }}">
          <div class="memberList">
            <template repeat="{{ function in cls.functions }}">
              <div class="memberItem">
                <div class="memberValue">
                  <function-ref ref="{{ function }}" qualified="{{ false }}">
                  </function-ref>
                </div>
              </div>
            </template>
          </div>
        </curly-block><br><br>
      </template>

      <template if="{{ !cls.hasNoAllocations }}">
        instances
          <div class="memberItem">
            <div class="memberName">currently allocated</div>
            <div class="memberValue">
              count {{ cls.newSpace.current.instances + cls.oldSpace.current.instances }}
              (shallow size {{ cls.newSpace.current.bytes + cls.oldSpace.current.bytes | formatSize }})
            </div>
          </div>
          <div class="memberItem">
            <div class="memberName">strongly reachable</div>
            <div class="memberValue">
              <template if="{{ instances == null }}">
                <eval-link callback="{{ reachable }}"
                           label="[find]"
                           expr="100">
                </eval-link>
              </template>
              <template if="{{ instances != null }}">
                <template repeat="{{ sample in instances['samples'] }}">
                  <any-service-ref ref="{{ sample }}">
                  </any-service-ref><br>
                </template>
                <template if="{{ instances['totalCount'] > instances['samples'].length }}">
                  <eval-link callback="{{ reachable }}"
                           label="[more]"
                           expr="{{ instances['samples'].length * 2 }}">
                  </eval-link>
                </template>
                of total {{ instances['totalCount'] }}
              </template>
            </div>
          </div>
          <div class="memberItem">
            <div class="memberName">total retained memory size</div>
            <div class="memberValue">
              <template if="{{ retainedBytes == null }}">
                <eval-link callback="{{ retainedSize }}"
                           label="[calculate]">
                </eval-link>
              </template>
              <template if="{{ retainedBytes != null }}">
                {{ retainedBytes | formatSize }}
              </template>
            </div>
          </div>
          <div class="memberItem">
            <div class="memberName">toplist by retained memory</div>
            <div class="memberValue">
              <template if="{{ mostRetained == null }}">
                <eval-link callback="{{ retainedToplist }}"
                           label="[find]">
                </eval-link>
              </template>
              <template if="{{ mostRetained != null }}">
                <template repeat="{{ most in mostRetained }}">
                  {{ most.retainedSize | formatSize }}<instance-ref ref="{{ most }}"></instance-ref><br>
                </template>
              </template>
            </div>
          </div>
        </template>
    </div>

    <hr>

    <div class="content-centered-big">
      <source-inset location="{{ cls.location }}">
      </source-inset>
    </div>

    <view-footer></view-footer>
  </template>
</polymer-element>

<script type="application/dart" src="class_view.dart"></script>
